package com.china.zhuoyue.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import com.china.zhuoyue.R
import com.china.zhuoyue.bean.IndexFun
import com.china.zhuoyue.databinding.FragemntIndexBinding
import com.china.zhuoyue.databinding.ItemIndexFunBinding
import com.china.zhuoyue.databinding.ItemIndexServiceBinding
import com.china.zhuoyue.ui.activity.commom.FeedbackActivity
import com.china.zhuoyue.ui.activity.commom.openUrl
import com.china.zhuoyue.ui.activity.estate.SmartOpenDoorActivity
import com.china.zhuoyue.ui.activity.estate.SmartStopCarActivity
import com.china.zhuoyue.ui.activity.estate.SwitchHomeActivity
import com.china.zhuoyue.ui.activity.estate.home.service.*
import com.china.zhuoyue.ui.activity.market.MarketMainActivity
import com.china.zhuoyue.ui.activity.user.MessageNoticeActivity
import com.china.zhuoyue.ui.base.BaseFragment
import com.china.zhuoyue.ui.base.BaseRecyclerAdapter
import com.china.zhuoyue.ui.base.BaseViewHolder
import com.china.zhuoyue.ui.ext.startActivityExt
import com.china.zhuoyue.ui.utils.FullyGridLayoutManager
import com.china.zhuoyue.ui.utils.GlideImageLoader
import com.china.zhuoyue.ui.utils.KotlinTransferJava
import com.china.zhuoyue.ui.viewmodel.MainViewModel
import com.youth.banner.BannerConfig

class IndexFragment : BaseFragment<FragemntIndexBinding>() {
    private val mMainViewModel: MainViewModel by activityViewModels()
    private val mIndexFunAdapter = IndexFunAdapter()
    private val mIndexServiceAdapter = IndexServiceAdapter()

    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        loadView {
            bindViewClick(
                ivMessage, clIndexServiceSmartOpen, clIndexServiceSmartStopCar,
                clIndexServiceMall, clIndexServiceLoc, tvLoc
            )
            val cover = mutableListOf<Int>().apply {
                add(R.mipmap.banner_index_1)
                add(R.mipmap.banner_index_2)
                add(R.mipmap.banner_index_3)
                add(R.mipmap.banner_index_4)
            }
            banner.run {
                setImageLoader(GlideImageLoader())
                setIndicatorGravity(BannerConfig.RIGHT)
                setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
                setDelayTime(2000)
                setImages(cover)
            }.start()

            val mMarqueeMessages = mutableListOf<String>().apply {
                add("刚刚,人民日报公布1张照片，震惊国人！")
                add("疫情防控通知")
//                add("卓越家园党群服务中心开展端午送粽子活动2")
            }
            KotlinTransferJava.marqueeViewStartWithList(mvMarquee, mMarqueeMessages)
            mvMarquee.setOnItemClickListener { position, textView -> startActivityExt<CommunityNoticeActivity>() }

            mIndexFunAdapter.datas = IndexFunAdapter.INDEX_FUN
            mIndexFunAdapter.setOnClickAdapterListenter(::onClickFun)
            rvFun.adapter = mIndexFunAdapter
            rvFun.layoutManager = FullyGridLayoutManager(requireContext(), 4)

            mIndexServiceAdapter.datas = IndexServiceAdapter.INDEX_SERVICE
            mIndexServiceAdapter.setOnClickAdapterListenter(::onClickService)
            rvService.adapter = mIndexServiceAdapter
            rvService.layoutManager = FullyGridLayoutManager(requireContext(), 4)
        }
    }

    override fun initData(rootView: View?, savedInstanceState: Bundle?) {
        mMainViewModel.changeHomeLiveData.observe(this) {
            vb.tvLoc.text = it
        }
    }

    private fun onClickFun(v: View?, position: Int) {
        when (mIndexFunAdapter.getItem(position).nameResId) {
            R.string.text_main_index_fun_1 -> startActivityExt<SmartStopCarActivity>()
            R.string.text_main_index_fun_2 -> startActivityExt<EstatePaymentActivity>()
            R.string.text_main_index_fun_3 -> startActivityExt<GoodsPassActivity>()
            R.string.text_main_index_fun_4 -> startActivityExt<DecorateApplyActivity>()
            R.string.text_main_index_fun_5 -> startActivityExt<RepairActivity>()
            R.string.text_main_index_fun_6 -> startActivityExt<FeedbackActivity>()
            R.string.text_main_index_fun_7 -> startActivityExt<ScoreActivity>()
            R.string.text_main_index_fun_8 -> startActivityExt<CommunityNoticeActivity>()
        }
    }

    private fun onClickService(v: View?, position: Int) {
        when (mIndexServiceAdapter.getItem(position).nameResId) {
            R.string.text_main_index_service_home -> context?.openUrl("https://bj.daojia.com/")
            R.string.text_main_index_service_industry_incubation -> context?.openUrl("http://www.shequ.edu.cn/SQResource/Resources")
            R.string.text_main_index_service_industry_up -> context?.openUrl("http://www.shequ.edu.cn/SQResource/Resources")
            R.string.text_main_index_service_old_market -> startActivityExt<MarketMainActivity>()
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.tvLoc -> launchStartActivityForResult(SwitchHomeActivity::class.java) {
                mMainViewModel.changeHomeLiveData.value = it.data?.getStringExtra(SwitchHomeActivity.KEY_SWITCH_HOME) ?: MainViewModel.DEFAULT_HOME
            }
            vb.ivMessage -> startActivityExt<MessageNoticeActivity>()
            vb.clIndexServiceSmartStopCar -> startActivityExt<SmartStopCarActivity>()
            vb.clIndexServiceSmartOpen -> startActivityExt<SmartOpenDoorActivity>()
            vb.clIndexServiceMall -> mMainViewModel.changeTabLiveData.value = 1
            vb.clIndexServiceLoc -> mMainViewModel.changeTabLiveData.value = 2
        }
    }

    override fun onStart() {
        super.onStart()
        vb.mvMarquee.startFlipping();
    }

    override fun onStop() {
        super.onStop()
        vb.mvMarquee.stopFlipping()
    }
}

class IndexFunAdapter : BaseRecyclerAdapter<BaseViewHolder<ItemIndexFunBinding>, IndexFun>() {
    companion object {
        val INDEX_FUN = mutableListOf<IndexFun>().apply {
            add(IndexFun(R.string.text_main_index_fun_1, R.mipmap.icon_index_fun_1))
            add(IndexFun(R.string.text_main_index_fun_2, R.mipmap.icon_index_fun_2))
            add(IndexFun(R.string.text_main_index_fun_3, R.mipmap.icon_index_fun_3))
            add(IndexFun(R.string.text_main_index_fun_4, R.mipmap.icon_index_fun_4))
            add(IndexFun(R.string.text_main_index_fun_5, R.mipmap.icon_index_fun_5))
            add(IndexFun(R.string.text_main_index_fun_6, R.mipmap.icon_index_fun_6))
            add(IndexFun(R.string.text_main_index_fun_7, R.mipmap.icon_index_fun_7))
            add(IndexFun(R.string.text_main_index_fun_8, R.mipmap.icon_index_fun_8))
        }
    }

    override fun onBindViewHolder(holder: BaseViewHolder<ItemIndexFunBinding>, bean: IndexFun, position: Int) {
        holder.vb.apply {
            ivImage.setImageResource(bean.iconResId)
            tvName.setText(bean.nameResId)
            setViewClick(position, root)
        }
    }
}

class IndexServiceAdapter : BaseRecyclerAdapter<BaseViewHolder<ItemIndexServiceBinding>, IndexFun>() {
    companion object {
        val INDEX_SERVICE = mutableListOf<IndexFun>().apply {
            add(IndexFun(R.string.text_main_index_service_home, R.mipmap.icon_index_service_home))
            add(IndexFun(R.string.text_main_index_service_industry_incubation, R.mipmap.icon_index_service_industry_incubation))
            add(IndexFun(R.string.text_main_index_service_industry_up, R.mipmap.icon_index_service_industry_up))
            add(IndexFun(R.string.text_main_index_service_old_market, R.mipmap.icon_index_service_old_market))
        }
    }

    override fun onBindViewHolder(holder: BaseViewHolder<ItemIndexServiceBinding>, bean: IndexFun, position: Int) {
        holder.vb.apply {
            ivImage.setImageResource(bean.iconResId)
            tvName.setText(bean.nameResId)
            setViewClick(position, root)
        }
    }
}